/**
 * www.jbarreto.com all rights reserved 2011
 */
package com.jbarreto.vc.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.jbarreto.vc.dataobjects.Pelicula;
import com.jbarreto.vc.dataobjects.PeliculaHome;
import com.jbarreto.vc.forms.PeliculaForm;
import com.jbarreto.vc.hibernate.HibernateAction;

/**
 * @author JBarreto
 * 
 *         jj_b_f@yahoo.com.mx
 * 
 *         14/01/2011 10:51:58
 */
public class MostrarPeliculaAction extends HibernateAction {

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.apache.struts.action.Action#execute(org.apache.struts.action.
	 * ActionMapping, org.apache.struts.action.ActionForm,
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		Session session = getSession(request);
		
		PeliculaForm peliculaForm = (PeliculaForm) form; 
		
		if(PeliculaForm.DISPLAY_PELICULA.equalsIgnoreCase(peliculaForm.getOperation()) && request.getParameter("peliculaID")!= null){
			loadPeliculaForm(peliculaForm, session, Integer.parseInt(request.getParameter("peliculaID")));
		}else if (PeliculaForm.CREATE_PELICULA.equalsIgnoreCase(peliculaForm.getOperation())) {
			createPelicula(peliculaForm, session);
			addMessage(request, "pelicula.createSuccess");
			return mapping.findForward("create-success");
		}else if (PeliculaForm.MODIFY_PELICULA.equalsIgnoreCase(peliculaForm.getOperation())) {
			modifyPelicula(peliculaForm, session);
			addMessage(request, "pelicula.updateSuccess");
			return mapping.findForward("modify-success");
		}else if (PeliculaForm.DELETE_PELICULA.equalsIgnoreCase(peliculaForm.getOperation()) && request.getParameter("peliculaID")!= null) {
			deletePelicula(Integer.parseInt(request.getParameter("peliculaID")), session);
			addMessage(request, "pelicula.deleteSuccess");
			return mapping.findForward("delete-success");
		}
		
		/* Close session */
		System.out.println("Hibernate Session Closed");
		session.close();
		peliculaForm.setOperation(PeliculaForm.CREATE_PELICULA);
		return mapping.findForward("success");
	}

	public boolean loadPeliculaForm(PeliculaForm peliculaForm, Session session, int peliculaId)
			throws Exception {
		boolean loaded = true;

		Pelicula pelicula = (Pelicula) session.get(Pelicula.class, peliculaId); // Criteria

		BeanUtils.copyProperties(peliculaForm, pelicula);
		peliculaForm.setOperation(PeliculaForm.MODIFY_PELICULA);

		return loaded;
	}
	
	public boolean createPelicula(PeliculaForm peliculaForm, Session session)
			throws Exception {
		boolean created = true;

		Transaction tx = session.beginTransaction();
		Pelicula pelicula = new Pelicula();

		BeanUtils.copyProperties(pelicula, peliculaForm);

		session.merge(pelicula);

		tx.commit();
		return created;
	}

	public boolean modifyPelicula(PeliculaForm peliculaForm, Session session)
			throws Exception {
		boolean modified = true;
		
		Transaction tx = session.beginTransaction();
		Pelicula pelicula = (Pelicula) session.get(Pelicula.class, peliculaForm.getId());

		BeanUtils.copyProperties(pelicula, peliculaForm);
		
		tx.commit();
		return modified;
	}

	public boolean deletePelicula(int peliculaId, Session session)
			throws Exception {
		boolean deleted = true;

		PeliculaHome peliculaHome = new PeliculaHome();
		peliculaHome.deleteById(peliculaId, session);

		return deleted;
	}

	private void addMessage(HttpServletRequest request, String message){
		ActionMessages messages = new ActionMessages();
		messages.add("message", new ActionMessage(message));
		saveMessages(request, messages);
	}
	private void copyInfo(PeliculaForm peliculaForm, Pelicula pelicula) {

		peliculaForm.setTitulo(pelicula.getTitulo());
		peliculaForm.setTituloOriginal(pelicula.getTituloOriginal());
		peliculaForm.setYearProduccion(pelicula.getYearProduccion());
		peliculaForm.setEstudio(pelicula.getEstudio());
		peliculaForm.setDuracion(pelicula.getDuracion());
		peliculaForm.setDirector(pelicula.getDirector());
		peliculaForm.setActores(pelicula.getActores());
		peliculaForm.setPaisOrigen(pelicula.getPaisOrigen());
		peliculaForm.setIdioma(pelicula.getIdioma());
		peliculaForm.setGenero(pelicula.getGenero());
		peliculaForm.setFechaEstreno(pelicula.getFechaEstreno());
		peliculaForm.setFormato(pelicula.getFormato());
		peliculaForm.setClasificacion(pelicula.getClasificacion());
		peliculaForm.setSeccion(pelicula.getSeccion());
		peliculaForm.setCostoRenta(pelicula.getCostoRenta());
		peliculaForm.setCosto(pelicula.getCosto());
	}

}
